<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;
use App\Logic\AccountingLogic;
use Illuminate\Support\Facades\DB;

//use App\Http\Controllers\Controller;

class AccountingController extends BaseController
{
    /*
     * 企业账薄列表
     */
    public function index(Request $request){
    	$where[] = ['a.del_flag','=',0];

        //公司名模糊搜索
        if ($request->input('nameLike')) {
            $where[] = ['e.company_name', 'like', "%{$request->input('nameLike')}%"];
        }
        //所属企业筛选
        if ($request->input('companyType')==2){
            $where[]=['e.agency_id','=',''];
            $where[]=['e.company_type','=','2'];
        }else if($request->input('companyType')==1){
            $where[]=['e.agency_id','<>',''];
            $where[]=['e.company_type','=','2'];
        }
        //时间筛选
        if ($request->input('createTime')==1){
            $where[] = [DB::raw(
                "TO_DAYS(DATE_FORMAT(NOW(),'%Y-%m-%d')) - TO_DAYS(DATE_FORMAT(a.create_date,'%Y-%m-%d'))"
            ),'<=','90'];
        }elseif ($request->input('companyTime')==2){
            $where[] = [DB::raw(
                "TO_DAYS(DATE_FORMAT(NOW(),'%Y-%m-%d')) - TO_DAYS(DATE_FORMAT(a.create_date,'%Y-%m-%d'))"
            ),'>','90'];
            $where[] = [DB::raw(
                "TO_DAYS(DATE_FORMAT(NOW(),'%Y-%m-%d')) - TO_DAYS(DATE_FORMAT(a.create_date,'%Y-%m-%d'))"
            ),'<=','180'];
        }elseif ($request->input('companyTime')==3){
            $where[] = [DB::raw(
                "TO_DAYS(DATE_FORMAT(NOW(),'%Y-%m-%d')) - TO_DAYS(DATE_FORMAT(a.create_date,'%Y-%m-%d'))"
            ),'>','180'];
            $where[] = [DB::raw(
                "TO_DAYS(DATE_FORMAT(NOW(),'%Y-%m-%d')) - TO_DAYS(DATE_FORMAT(a.create_date,'%Y-%m-%d'))"
            ),'<=','365'];
        }elseif ($request->input('companyTime')==4){
            $where[] = [DB::raw(
                "TO_DAYS(DATE_FORMAT(NOW(),'%Y-%m-%d')) - TO_DAYS(DATE_FORMAT(a.create_date,'%Y-%m-%d'))"
            ),'>','365'];
        }
    	$accountings = AccountingLogic::getAccountingList($where);
    	return $this->view(null,compact('accountings'));
    }
	
	/**
	 * 企业账单薄详情
	 * return string
	 */
	public function details(Request $request,$id){
		$where[] = ['a.del_flag','=',0];		
	    if($id){
	    	$where[] = ['a.id','=',$id];
	    }
	    
	    $accounting = AccountingLogic::getAccountingDetails($where);
	    
	    return $this->view(null,compact('accounting'));
	}
}
